package com.chaodev.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.model.DataPageVO;
import com.chaodev.base.model.wenjuan.*;
import com.chaodev.base.service.WenJuanService;
import com.chaodev.base.dao.WenJuanDao;
import com.chaodev.base.dto.WenJuanPDto;
import com.chaodev.base.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * vcbase
 *
 * @author 95634
 * @描述
 * @date 2023/5/23 13:44
 **/
@Service
public class WenJuanServiceImpl implements WenJuanService {

    @Autowired
    private WenJuanDao wenJuanDao;

    @Override
    public PageInfo<Paper> examPaperQuery(int currentPage, int pageSize, String usercode) {
        PageHelper.startPage(currentPage,pageSize);
        List<Paper> papers = wenJuanDao.examPaperQuery(usercode);
        PageInfo<Paper> pageInfo = new PageInfo<>(papers, pageSize);
        return pageInfo;
    }

    /**
     * @description:检查试题名称是否重复
     * @param userInfo
     * @return
     */
    @Override
    public boolean isExist(UserInfo userInfo) {
        return wenJuanDao.isExist(userInfo) > 0;
    }

    @Override
    public boolean isAnswer(Answer answer) {
        return wenJuanDao.isAnswer(answer) > 0;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void save(Paper wenJuan) {
         wenJuanDao.insertData(wenJuan);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void update(Paper wenJuan) {
        wenJuanDao.updateDataById(wenJuan);
    }

    @Override
    public Paper editExamPaper(String id) {
        return wenJuanDao.editExamPaper(id);

    }
    @Override
    public List<Map<String,Object>> editExamPapers(String id) {
        return wenJuanDao.editExamPapers(id);
    }
    @Override
    public PageInfo<Map<String,Object>> myExamPaperQuery(DataPageVO dataPageVO) {
        PageHelper.startPage(dataPageVO.getPage(),dataPageVO.getRows());
        List<Map<String,Object>> list = wenJuanDao.myExamPaperQuery(dataPageVO);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(list, dataPageVO.getRows());
        return pageInfo;
    }

    @Override
    public boolean submitWenJuan(PaperFinsh paperFinsh) {
        List<PaperFinsh> paperFinshList = new ArrayList<>();
        paperFinshList.add(paperFinsh);
        return wenJuanDao.submitWenJuan(paperFinshList) != 0;
    }

    @Override
    public boolean submitWenJuan(List<PaperFinsh> paperFinshs) {
        return wenJuanDao.submitWenJuan(paperFinshs) != 0;
    }

    @Override
    public boolean submitWenJuanScore(UserScore userScore) {
        return wenJuanDao.submitWenJuanScore(userScore) != 0;
    }

    @Override
    public boolean saveQuestion(Question question, String paperId) {
        if(wenJuanDao.saveQuestion(question) != 0)
        {
            PaperQuestion paperQuestion = new PaperQuestion();
            paperQuestion.setCreateUser(question.getCreateUser());
            paperQuestion.setAreaCode(question.getAreaCode());
            paperQuestion.setQuestionId(question.getId());
            paperQuestion.setPaperId(Integer.valueOf(paperId));
            return wenJuanDao.savePaperQuestion(paperQuestion) != 0;
        }
        else{
            return false;
        }

    }

    @Override
    public boolean saveQuestion(Question question, String paperId, WenJuanPDto wenJuanPDto) {
        //保存问题成功
        if(wenJuanDao.saveQuestion(question) != 0)
        {
            List<Answer> answers = wenJuanPDto.getAnswers();
            //保存选项
            for(int i=0;i<answers.size();i++){
                Answer answer = answers.get(i);
                answer.setQuestionId(question.getId());
                answer.setAreaCode(question.getAreaCode());
                answer.setCreateUser(question.getCreateUser());
            }
            wenJuanDao.saveAnswers(answers);
            //保存到问卷
            PaperQuestion paperQuestion = new PaperQuestion();
            paperQuestion.setCreateUser(question.getCreateUser());
            paperQuestion.setAreaCode(question.getAreaCode());
            paperQuestion.setQuestionId(question.getId());
            paperQuestion.setPaperId(Integer.valueOf(paperId));
            return wenJuanDao.savePaperQuestion(paperQuestion) != 0;
        }
        else{
            return false;
        }

    }

    @Override
    public boolean bindPaper(List<PaperDepart> paperDeparts) {
        return wenJuanDao.bindPaper(paperDeparts) > 0;
    }

    @Override
    public PageInfo<Map<String,Object>> getPaperInfo(DataPageVO dataPageVO) {
        PageHelper.startPage(dataPageVO.getPage(),dataPageVO.getRows());
        List<Map<String,Object>> list = wenJuanDao.getPaperInfo(dataPageVO);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(list, dataPageVO.getRows());
        return pageInfo;
    }

    @Override
    public boolean setBindPaper(String paperId) {
        return wenJuanDao.setBindPaper(paperId) > 0;
    }

    @Override
    public boolean effectivePaper(String paperId) {
        return wenJuanDao.effectivePaper(paperId) == 1;
    }



    @Override
    public boolean updateQuestion(Question question, String paperId, WenJuanPDto wenJuanPDto) {
        //保存问题成功
        if(wenJuanDao.updateQuestion(question) != 0)
        {
            List<Answer> answers = wenJuanPDto.getAnswers();
            //保存选项,先修改已有的选项状态，用于处理删除选项的情况
            wenJuanDao.updateSql(question.getId());
            for(int i=0;i<answers.size();i++){
                Answer answer = wenJuanDao.selectAnswerById(String.valueOf(answers.get(i).getId()));
                if(null==answer){
                    answer = new Answer();
                    answer.setCreateUser(question.getCreateUser());
                    answer.setQuestionId(question.getId());
                    answer.setAnswerName(answers.get(i).getAnswerName());
                    answer.setAnswer(answers.get(i).getAnswer());
                    wenJuanDao.saveAnswer(answer);
                }else {
                    answer.setAnswerName(answers.get(i).getAnswerName());
                    answer.setState(1);
                    answer.setAnswer(answers.get(i).getAnswer());
                    wenJuanDao.updateAnswer(answer);
                }
            }

            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public boolean updateQuestion(Question question, String paperId) {
        return wenJuanDao.updateQuestion(question) != 0;
    }

    @Override
    public boolean deleteQuestionFromPaper(Question question, String paperId) {
        PaperQuestion paperQuestion = new PaperQuestion();
        paperQuestion.setPaperId(Integer.valueOf(paperId));
        paperQuestion.setQuestionId(question.getId());
        paperQuestion = wenJuanDao.selectPaperQuestionDetail(paperQuestion);
        if(null!=paperQuestion)
        {
            return wenJuanDao.deletePaperQuestion(String.valueOf(paperQuestion.getId())) != 0;
        }
        return false;
    }

    @Override

    public Integer paperQuestion(String id) {
        return wenJuanDao.paperQuestion(id);
    }

    @Override
    public boolean examPaperDel(String id) {
        return wenJuanDao.deleteDataById(id) != 0;
    }

    @Override
    public boolean sendExamPaper(String depart_id,String paperid) {
        PaperDepart paperDepart = new PaperDepart();
        paperDepart.setDepartId(depart_id);
        paperDepart.setPaperId(paperid);
        wenJuanDao.insertExamPaper(paperDepart);
        wenJuanDao.updateExamPaper(paperDepart);
        return true;
    }

    @Override
    public Paper getExamPaper(String id) {
        return wenJuanDao.getExamPaper(id);
    }

    @Override
    public List<UserScore> getUserScoreList(UserScore userScore) {
        return wenJuanDao.getUserScoreList(userScore);
    }

    @Override
    public List<Map<String,Object>> checkBing(Map<String,Object> map){
        return wenJuanDao.checkBing(map);
    }

    @Override
    public String getPaPerScoreResult(Map<String,Object> map){
        wenJuanDao.getPaPerScoreResult(map);
        return map.containsKey("rt")?map.get("rt").toString():"";
    }
}
